﻿// Holds a selector to be passed to the items of a container so the items and nested items can be selected
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

   return Wjs.defineOnce({

        type: 'SelectorHolder',

        setSelector: function (selector) {

            if (this.selectionChangedHandler) {

                selector.onSelectionChanged(this.selectionChangedHandler); // Attach the handler for the holder
            }

            this.selector = selector;

            return this;
        },

        getSelector: function () {

            return this.selector;
        },

        // Called when the child is added to the parent
        itemAddedHandler: function (child) {
            var selector = this.getSelector();

            // Pass the selector to the child
            if (selector) {

                selector.setupSelector(child);
            }
        },

        acceptChanges: function () {
            var selector = this.getSelector();

            if (selector) {

                selector.acceptChanges();
            }
        },

        isModified: function () {
            var selector = this.getSelector();

            if (selector) {

                return selector.isModified();
            }
        },

        isMultipleSelection: function () {
            var selector = this.getSelector();

            if (selector) {

                return selector.multipleSelection;
            }
        },

        getSelection: function () {
            var selector = this.getSelector();

            if (!selector) {

                throw 'Error: Selector not set in this view';
            }

            return selector.getSelection();
        }
    });

});